package com.ken.util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class printfUtl {
    public static void printfJson(String json){
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(json);

            // 遍历pages -> lines -> words 获取content
            if (rootNode.has("pages")) {
                for (JsonNode pageNode : rootNode.get("pages")) {
                    if (pageNode.has("lines")) {
                        for (JsonNode lineNode : pageNode.get("lines")) {
                            if (lineNode.has("words")) {
                                for (JsonNode wordNode : lineNode.get("words")) {
                                    if (wordNode.has("content")) {
                                        System.out.println(wordNode.get("content").asText());
                                    }
                                }
                            }

                            // 如果需要同时提取word_units中的content
//                            if (lineNode.has("word_units")) {
//                                for (JsonNode unitNode : lineNode.get("word_units")) {
//                                    if (unitNode.has("content")) {
//                                        System.out.println(unitNode.get("content").asText());
//                                    }
//                                }
//                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
